package org.example.gui.model;

import org.example.dao.CategoryDAO;
import org.example.entity.Category;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.text.DecimalFormat;
import java.util.List;

/**
 * CategoryPanel 的 Table 的数据模型，实现了 TableModel 接口
 */

public class CategoryTableModel implements TableModel {
    //列
    private final String[] columnNames = new String[]{"分类名称", "消费总次数", "消费总金额"};
    //行数据
    public List<Category> categoryList = CategoryDAO.listAllCategory();

    @Override
    public int getRowCount() {
        return categoryList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (0 == columnIndex)
            return categoryList.get(rowIndex).getName();
        if (1 == columnIndex)
            return categoryList.get(rowIndex).getRecordNumber();
        if (2 == columnIndex) {
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(categoryList.get(rowIndex).getRecordTotalSpend());
        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }
}